/*******************************************************************************
																				
	DESCRIPTION:  	This do file produces Appendix Table A9.
	
*******************************************************************************/

clear all
global id_code 134
set seed 2110

* Import estimates:
estimates use "${output}/${id_code}_TwoSpell_Analysis_SplitByBetaD.ster"

* Write appendix LaTeX table:
file open myfile using "${output}/${id_code}_TwoSpells_Table_SplitByBetaD.tex", write replace

file write myfile "\documentclass{article}" _newline ///
	"\usepackage{multirow}" _newline ///
	"\usepackage{booktabs}" _newline ///	
	"\usepackage[margin=1in]{geometry}" _newline ///
	"\begin{document}" _newline ///
	"\begin{table}[h] \centering" _newline ///
	"\begin{tabular}{@{\extracolsep{8pt}} l c c c c}" _newline ///
	"\hline \hline \addlinespace[0.2cm]" _newline ///


foreach x in "NonMissing" "BelowMedian" "AboveMedian" {

	if "`x'" == "NonMissing" {
		local title "A. Individuals in the 2006 holdout sample (\( n = `: di %9.0fc _b[`x':n]' \))"
	}
	
	if "`x'" == "BelowMedian" {
		local title "B. Below Median $\beta_D(X)$ (\( n = `: di %9.0fc _b[`x':n]' \))"
	}	
	
	if "`x'" == "AboveMedian" {
		local title "C. Above Median $\beta_D(X)$ (\( n = `: di %9.0fc _b[`x':n]' \))"
	}
	
		* Export table to csv:
	cap frame drop tabl	
	frame create tabl str30(obs) double(mult_pers mult_trans mult_total ref_total)
	frame post tabl ("Observable") (_b[`x':v_obs_pers]) (_b[`x':v_obs_trans]) (_b[`x':v_obs]) (_b[`x':v_obs_control])
	frame post tabl ("") (_se[`x':v_obs_pers]) (_se[`x':v_obs_trans]) (_se[`x':v_obs]) (_se[`x':v_obs_control])
	frame post tabl ("Unobservable") (_b[`x':v_unobs_pers]) (.) (.) (.)
	frame post tabl ("") (_se[`x':v_unobs_pers]) (.) (.) (.)
	frame post tabl ("Total") (_b[`x':v_pers]) (.) (_b[`x':v]) (_b[`x':v_control])
	frame post tabl ("") (_se[`x':v_pers]) (.) (_se[`x':v]) (_se[`x':v_control])
	frame tabl: export delimited using "${output}/${id_code}_TwoSpells_Table_SplitByBetaD_`x'.csv", replace

	
	* Write to table:
	file write myfile ///
		"& \multicolumn{4}{c}{\large \textbf{`title'}} \\ \addlinespace[1.5ex]" _newline ///
		"& \multicolumn{3}{c}{Multiple Spells Sample} & Reference Sample \\  \addlinespace[0.1cm]" _newline ///
		"\cline{2-4} \cline{5-5} \addlinespace[0.1cm]" _newline ///
		"\hspace{2cm} & \textbf{Persistent} & \textbf{Transitory} & \textbf{Total} & \textbf{Total} \\ \addlinespace[0.2cm]" _newline ///
		"\multirow{2}{*}{\textbf{Observable}} & \(POH = Cov_{0,0}^{t_1,t_2}(\hat{F}_{0}^{t_1}, F_{0}^{t_2})\) & \(OH - POH \)  & \( OH = Cov_{0,0}^{t_1,t_2}(\hat{F}_{0}^{t_2}, F_{0}^{t_2})\) & \( \tilde{OH} = Cov_{0}^{t_2}(\hat{F}_{0}^{t_2}, F_{0}^{t_2})\) \\"  _newline ///
		" & \textbf{`: di %5.3f _b[`x':v_obs_pers]'} & \textbf{`: di %5.3f _b[`x':v_obs_trans]'} & \textbf{`: di %5.3f _b[`x':v_obs]'} & \textbf{`: di %5.3f _b[`x':v_obs_control]'} \\" _newline ///
		" & (`: di %5.4f _se[`x':v_obs_pers]') & (`: di %5.4f _se[`x':v_obs_trans]') & (`: di %5.4f _se[`x':v_obs]') & (`: di %5.4f _se[`x':v_obs_control]') \\ \addlinespace[0.2cm]" _newline ///
		"\multirow{2}{*}{\textbf{Unobservable}} & \( PH - POH \) & & &  \\" _newline ///
		" & \textbf{`: di %5.3f _b[`x':v_unobs_pers]'} & & & \\" _newline ///
		" & (`: di %5.4f _se[`x':v_unobs_pers]') & & & \\ \addlinespace[0.2cm]" _newline ///
		"\cline{2-4} \cline{5-5} \addlinespace[0.2cm]" _newline ///
		"\multirow{2}{*}{\textbf{Total}} & \(PH = Cov_{0,0}^{t_1,t_2}(F_{0}^{t_1}, F_{0}^{t_2}) \) & & \textit{By Proportionality} & \textit{By Proportionality} \\" _newline ///
		"& \textbf{`: di %5.3f _b[`x':v_pers]'} & & \textit{`: di %5.3f _b[`x':v]'} & \textit{`: di %5.3f _b[`x':v_control]'} \\ " _newline ///
		"& (`: di %5.4f _se[`x':v_pers]') & & \textit{(`: di %5.4f _se[`x':v]')} & \textit{(`: di %5.4f _se[`x':v_control]')} \\  \addlinespace[0.2cm]" _newline ///
		"\addlinespace[6ex]" _newline ///

}	

file write myfile ///
	"\addlinespace[-5ex]" _newline ///
	"\hline \hline" _newline ///
	"\end{tabular}" _newline ///
	"\end{table}" _newline ///
	"\end{document}"

file close myfile
